<?php

namespace App\Console\Commands\Tools;

use App\Kuafu\Pay\BasePay;
use App\Kuafu\Pay\Channel\FuYou\FuYouBase;
use App\Kuafu\Pay\Service\PayService;
use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Model\CameraModel;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Model\ShopMonitorDataModel;
use App\Kuafu\Shop\Service\CameraLibrary\CameraFactory;
use App\Kuafu\Shop\Service\ShopCameraSyncService;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SyncYlkPassengerFlow extends Command
{
    protected $signature = 'tools:sync-ylk-passenger-flow';

    protected $description = "同步悠络客摄像头的客流数据";

    public function handle()
    {
        // 获取客流相机数据
        CameraModel::query()
                   ->where('deviceType', CameraModel::DEVICE_TYPE_CAMERA)
                   ->where('supplier', CameraModel::SUPPLIER_YLK)
                   ->where('type', CodeEnum::SHOP_CAMERA_TYPE_1)
                   ->get()
                   ->each(function ($item) {
                       // 只针对悠络客摄像头单独处理数据
                       $camera = new CameraFactory($item->id);

                       $date          = date('Y-m-d', strtotime('-1 day'));
                       $dateTimestamp = strtotime($date);

                       $data = $camera->getIns()->getStatisticHourlySummary($date);

                       if (!empty($data) || !empty($data['items'])) {
                           // 删除历史数据
                           ShopMonitorDataModel::query()
                                               ->where('shopId', $item->shopId)
                                               ->whereBetween('dataAt', [$dateTimestamp, $dateTimestamp + 86400])
                                               ->whereIn('type', [
                                                   ShopMonitorDataModel::TYPE_IN_SHOP,
                                                   ShopMonitorDataModel::TYPE_PASS_SHOP
                                               ])
                                               ->delete();

                           $insertData = [];
                           foreach ($data['items'] as $datum) {
                               if (empty($datum['enter_count']) || empty($datum['pass_count'])) {
                                   continue;
                               }

                               $insertData[] = [
                                   'shopId'    => $item->shopId,
                                   'monitorId' => $item->id,
                                   'type'      => ShopMonitorDataModel::TYPE_IN_SHOP,
                                   'dataAt'    => $dateTimestamp + $datum['hour'] * 3600 + 1,
                                   'count'     => $datum['enter_count'],
                                   'createdAt' => time(),
                                   'updatedAt' => time(),
                               ];

                               $insertData[] = [
                                   'shopId'    => $item->shopId,
                                   'monitorId' => $item->id,
                                   'type'      => ShopMonitorDataModel::TYPE_PASS_SHOP,
                                   'dataAt'    => $dateTimestamp + $datum['hour'] * 3600 + 1,
                                   'count'     => $datum['pass_count'],
                                   'createdAt' => time(),
                                   'updatedAt' => time(),
                               ];
                           }

                           ShopMonitorDataModel::query()->insert($insertData);
                       }
                   });
    }
}
